<template>
	<el-drawer :title="`《`+form.flowName+`》隐藏字段设置`" v-model="visible" size="50%" destroy-on-close @closed="$emit('closed')">
		<el-container v-loading="loading">
			<el-header>
				<div class="left-panel">
					<el-button type="primary" icon="el-icon-plus" @click="setReadFieldRole">创建</el-button>
					<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
				</div>
			</el-header>
			<el-main>
				<scTable ref="table" :apiObj="apiObj" :params="{flowId:form.flowId}" @selection-change="selectionChange" row-key="recordId" stripe remoteSort remoteFilter>
					<el-table-column type="selection" width="50"></el-table-column>
					<el-table-column label="#" type="index" width="50"></el-table-column>
					<el-table-column label="姓名" prop="userName" width="100"></el-table-column>
					<el-table-column label="隐藏字段" prop="hideField" width="300" :show-overflow-tooltip="true">
						<template #default="scope">
							{{getFieldName(scope.row)}}
						</template>
					</el-table-column>
					<el-table-column label="创建时间" prop="createTime" width="150"></el-table-column>
					<el-table-column label="备注" prop="remark" width="150" :show-overflow-tooltip="true"></el-table-column>
					<el-table-column label="操作" fixed="right" align="right" width="170">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
								<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
								<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
									<template #reference>
										<el-button text type="primary" size="small">删除</el-button>
									</template>
								</el-popconfirm>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-drawer>
	<readFieldRoleDialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></readFieldRoleDialog>
</template>

<script>
import readFieldRoleDialog from './readFieldRoleDialog'
export default {
	emits: ['success', 'closed'],
	components: {
		readFieldRoleDialog
	},
	data() {
		return {
			dialog: {
				save: false,
			},
			loading: false,
			form: {
				formId:"",
				flowId: "",
				flowName:"",
			},
			hideField:[],
			selection:[],
			apiObj: this.$API.bpm.bpmReadFieldRole.getAllBpmReadFieldRoleList,
			visible: false,
			isSaveing: false,
		}
	},
	mounted() {

	},
	methods: {
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		table_show(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open("show").setData(row)
			})
		},
		async table_del(row) {
			var reqData = {recordId: row.recordId}
			var res = await this.$API.bpm.bpmReadFieldRole.deleteBpmReadFieldRole.post(reqData);
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//显示
		open() {
			this.visible = true;
			return this;
		},
		//表单注入数据
		setData(data) {
			this.form.formId  = data.formId
			this.form.flowId = data.flowId
			this.form.flowName = data.flowName
			this.getBpmFormFieldsListForSelect();
		},
		setReadFieldRole(){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open().createSetData({flowId:this.form.flowId,formId:this.form.formId})
			})
		},
		handleSaveSuccess() {
			this.$refs.table.refresh()
		},
		async batch_del() {
			await this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async() => {
				var recordIdArr = [];
				for (var i = 0; i < this.selection.length; i++) {
					recordIdArr.push(this.selection[i].recordId);
				}
				const loading = this.$loading();
				var res = await this.$API.bpm.bpmReadFieldRole.deleteBpmReadFieldRoleByIds.post({ids: recordIdArr});
				if (res.code == 200) {
					this.$refs.table.refresh()
					this.$message.success(res.message)
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
				loading.close();
			}).catch(() => {

			})
		},
		async getBpmFormFieldsListForSelect() {
			var res = await this.$API.bpm.bpmForm.getBpmFormFieldsListForSelect.get({formId: this.form.formId});
			if (res.code == 200) {
				this.formFieldList = res.data;
			}
		},
		getFieldName(row)
		{
			let field = row.hideField;
			if(field==undefined||field=='')
			{
				return ""
			}else{
				let nameArr=[];
				let fArr = field.split(",");
				this.formFieldList.forEach(item=>{
					for(let i=0;i<fArr.length;i++)
					{
						if(fArr[i]==item.id)
						{
							nameArr.push(item.label);
						}
					}
				})
				return nameArr.join(",");
			}
		}
	}
}
</script>

<style>
</style>
